package com.paradise.ytj.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paradise.ytj.R;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephoneListener;

import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCallStats;
import org.linphone.core.PayloadType;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/10/11 0011.
 */

public class StateVideoFragment extends Fragment {

    private View inflate;
    private TextView tvCode;
    private TextView tvUpload;
    private TextView tvDownload;
    private TextView tvReceivePacket;
    private TextView tvSendDip;
    private boolean is = true;
    private TextView tvSendPacket;
    private Activity context;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                LinphoneCall currentCall = FacePhone.instance().getmLc().getCurrentCall();
                if (currentCall != null) {
                    PayloadType codec = currentCall.getCurrentParams().getUsedVideoCodec();
                    if (codec != null) {
                        tvCode.setText(codec.getMime() + "/" + codec.getRate() / 1000 + "kHz");
                    }
                    LinphoneCallStats videoStats = currentCall.getVideoStats();
                    if (videoStats != null) {
                        tvUpload.setText((int) videoStats.getUploadBandwidth() + "kbits/s");
                        tvDownload.setText((int) videoStats.getDownloadBandwidth() + "kbits/s");
                        tvReceivePacket.setText(String.format("%.1f", videoStats.getReceiverLossRate()) + "%");
                        tvSendPacket.setText(String.format("%.1f", videoStats.getSenderLossRate()) + "%");
                        tvReceivePacket.setText(videoStats.getReceiverLossRate() + "%");
                        tvSendPacket.setText(videoStats.getSenderLossRate() + "%");
                        tvFpsS.setText(String.format("%.1f", currentCall.getCurrentParams().getSentFramerate()));
                        tvFpsR.setText(String.format("%.1f", currentCall.getCurrentParams().getReceivedFramerate()));
                        tvSendDip.setText(currentCall.getCurrentParams().getSentVideoSize().width + "*" +
                                currentCall.getCurrentParams().getSentVideoSize().height);
                        tvReceiveDip.setText(currentCall.getCurrentParams().getReceivedVideoSize().width + "*" +
                                currentCall.getCurrentParams().getReceivedVideoSize().height);
                        tvEstimated.setText(String.format("%.1f", videoStats.getEstimatedDownloadBandwidth()) + "kbits/s");
                    }

                    PayloadType codeaudio = currentCall.getCurrentParams().getUsedAudioCodec();
                    if (codeaudio != null) {
                        tvAudioCode.setText(codeaudio.getMime() + "/" + codeaudio.getRate() / 1000 + "kHz");
                    }
                    LinphoneCallStats audioStats = currentCall.getAudioStats();
                    if (audioStats != null) {
                        tvAudioUpload.setText((int) audioStats.getUploadBandwidth() + "kbits/s");
                        tvAudioDownload.setText((int) audioStats.getDownloadBandwidth() + "kbits/s");
                        tvAudioReceivePacket.setText(audioStats.getReceiverLossRate() + "%");
                        tvAudioSendPacket.setText(audioStats.getSenderLossRate() + "%");
                        tvAudioEcho.setText(audioStats.getJitterBufferSize() + "ms");
                    }
                }
            }
            return false;
        }
    });
    private TextView tvFpsS;
    private TextView tvFpsR;
    private TextView tvReceiveDip;

    private TextView tvAudioCode;
    private TextView tvAudioUpload;
    private TextView tvAudioDownload;
    private TextView tvAudioReceivePacket;
    private TextView tvAudioSendPacket;
    private TextView tvAudioEcho;
    private TextView tvEstimated;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        inflate = View.inflate(context, R.layout.fragment_state, null);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        tvCode = (TextView) inflate.findViewById(R.id.tv_code_value);
        tvUpload = (TextView) inflate.findViewById(R.id.tv_upload_value);
        tvDownload = (TextView) inflate.findViewById(R.id.tv_download_value);
        tvReceivePacket = (TextView) inflate.findViewById(R.id.tv_receive_packet_value);
        tvSendPacket = (TextView) inflate.findViewById(R.id.tv_send_packet_value);
        tvSendDip = (TextView) inflate.findViewById(R.id.tv_sendDip_value);
        tvReceiveDip = (TextView) inflate.findViewById(R.id.tv_receiveDip_value);
        tvFpsS = (TextView) inflate.findViewById(R.id.tv_fps_send_value);
        tvFpsR = (TextView) inflate.findViewById(R.id.tv_fps_revice_value);

        tvAudioCode = (TextView) inflate.findViewById(R.id.tv_audio_code_value);
        tvAudioUpload = (TextView) inflate.findViewById(R.id.tv_audio_upload_value);
        tvAudioDownload = (TextView) inflate.findViewById(R.id.tv_audio_download_value);
        tvAudioReceivePacket = (TextView) inflate.findViewById(R.id.tv_audio_receive_packet_value);
        tvAudioSendPacket = (TextView) inflate.findViewById(R.id.tv_audio_send_packet_value);
        tvAudioEcho = (TextView) inflate.findViewById(R.id.tv_audio_echo_value);
        tvEstimated = (TextView) inflate.findViewById(R.id.tv_estimated_download_value);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (is) {
                    SystemClock.sleep(1500);
                    handler.sendEmptyMessage(0);
                }

            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        is = false;
    }

}
